using Microsoft.AspNetCore.Mvc;
using SqlKata;
using SqlKata.Execution;
using SqlKataDemo.WebApi.Models;

namespace SqlKataDemo.WebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OrderController : ControllerBase
    {
        private readonly QueryFactory _queryFactory;

        public OrderController(QueryFactory queryFactory, ILogger<OrderController> logger)
        {
            queryFactory.Logger = result =>
            {
                logger.LogDebug(result.Sql);
            };
            _queryFactory = queryFactory;
        }

        [HttpGet]
        public Order Get()
        {
            var query = new Query("order");
            var order = _queryFactory.Get<Order>(query).FirstOrDefault();
            return order;
        }
    }
}